<%@ Page Language="C#" %>

<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Microsoft.Win32" %>
<script runat="server">
    
    string fileName;
    protected void Page_Load(object sender, EventArgs e)
    {
        int pid = int.Parse(Request.QueryString["pid"].ToString());
        fileName = Server.UrlDecode(Request.QueryString["filename"].ToString());
        long ticks = Convert.ToInt64(Request.QueryString["salt"].ToString());
        string filePath = ConfigurationSettings.AppSettings["CMS_Path"].ToString();
        filePath += @"\project_files\" + pid + @"\" + ticks + @"_" + fileName;
        FileInfo file = new FileInfo(filePath);
        if (file != null)
        {
            DownloadFile(file);
        }
    }

    private void DownloadFile(FileInfo file)
    {
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        Response.ContentType = GetContentTypeFromFileExt(file.Extension);
        //Response.AppendHeader("Content-Disposition", "attachment; filename=\"web.config\"");
        //Response.ContentType = GetContentTypeFromFileExt("config");
        
        // Can't download large file, and will cause server unresponding due to remory exhaust
        //Response.Flush();
        //Response.WriteFile(file.FullName);

        Stream iStream = null;
        // Buffer to read 4K bytes in chunk:
        byte[] buffer = new Byte[4096];
        // Length of the file:
        int length;
        // Total bytes to read:
        long dataToRead;
        
        try
        {            
            // Open file to read
            iStream = file.OpenRead();
            dataToRead = iStream.Length;
            while (dataToRead > 0)
            {
                // Verify that client is connected
                if (Response.IsClientConnected)
                {
                    // Read data in buffer
                    length = iStream.Read(buffer, 0, 4096);
                    // Write data to current output stream
                    Response.OutputStream.Write(buffer, 0, length);
                    // Flush data to HTML output
                    Response.Flush();
                    buffer = new Byte[4096];
                    dataToRead -= length;
                }
                else
                {
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            
        }
        finally
        {
            if (iStream != null)
            {
                // Close the file
                iStream.Close();
            }
        }
        
    }

    private static string GetContentTypeFromFileExt(string fileExtension)
    {
        RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(fileExtension);
        try
        {
            return regKey.GetValue("Content Type", "application/octet-stream").ToString();
        }
        catch (Exception)
        {
            return "application/octet-stream";
        }
    }

</script>
